import os

template_py = r"""import os
import sys
from pathlib import Path
import yaml

BASE_DIR = Path(__file__).resolve().parent.parent


class ImproperlyConfigured(Exception):
    "Server is somehow improperly configured"
    pass


CONFIG_PATH = os.path.join(BASE_DIR, 'config.yaml')
# 从配置文件更新
if os.path.exists(CONFIG_PATH):
    with open(CONFIG_PATH, "r") as f:
        config_dict = yaml.load(f.read(), Loader=yaml.FullLoader)
        if config_dict is None:
            config_dict = {}
else:
    sys.stderr.write(f"请确认配置文件{CONFIG_PATH}是否存在!! 配置文件信息请见 README.MD#Configuration\n")


def _get_config(field, default=None):
    if field in config_dict.keys():
        return config_dict[field]
    else:
        if default is None:
            raise ImproperlyConfigured(f"缺失必须的配置信息 {field}")
        else:
            sys.stderr.write(f"缺失配置信息{field}，现使用默认值{default}.\n")
            return default

NAME = _get_config("NAME")
"""
template_yaml = """NAME : template"""


def make_config():
    if not os.path.exists("./config"):
        os.mkdir('config')
    if not os.path.exists("./config/Config.py"):
        with open("config/Config.py", 'w+') as w:
            w.write(template_py)
    if not os.path.exists("./config.yaml"):
        with open("config.yaml", "w+") as w:
            w.write(template_yaml)


if __name__ == '__main__':
    make_config()
